﻿using System.Globalization;

namespace BlazorSerialUpload.Common;

public class HexToBin
{
    public HexToBin.ExecResult HEX_ConvertBin(
      string hexPath,
      ref uint destAddr,
      ref byte[] binBuffer)
    {
        string str1 = "";
        uint num = 0;
        if (hexPath == "")
            return HexToBin.ExecResult.ExecErrorParameter;
        destAddr = uint.MaxValue;
        StreamReader streamReader = new StreamReader(hexPath);
        while (true)
        {
            string str2;
            do
            {
                do
                {
                    str2 = streamReader.ReadLine();
                    if (str2 == null)
                        goto label_12;
                }
                while (!(str2.Substring(0, 1) == ":"));
                if (!(str2.Substring(7, 2) == "01"))
                {
                    if (str2.Substring(7, 2) == "00")
                    {
                        if (uint.MaxValue == destAddr)
                        {
                            string str3 = str2.Substring(3, 4);
                            destAddr = Convert.ToUInt32(str3, 16);
                            destAddr |= num;
                        }
                        str1 += str2.Substring(9, str2.Length - 11);
                    }
                }
                else
                    goto label_12;
            }
            while (!(str2.Substring(7, 2) == "04"));
            ushort uint16 = Convert.ToUInt16(str2.Substring(1, 2), 16);
            num = Convert.ToUInt32(str2.Substring(9, (int)uint16 * 2), 16) << 16;
        }
    label_12:
        streamReader.Close();
        binBuffer = new byte[str1.Length / 2];
        int hex = (int)HexToBin.StringToHex(str1, ref binBuffer);
        return HexToBin.ExecResult.ExecOk;
    }

    public HexToBin.ExecResult SREC_ConvertBin(
      string srecPath,
      ref uint destAddr,
      ref byte[] binBuffer)
    {
        string str1 = "";
        if (srecPath == "")
            return HexToBin.ExecResult.ExecErrorParameter;
        destAddr = uint.MaxValue;
        StreamReader streamReader = new StreamReader(srecPath);
    label_3:
        string str2;
        do
        {
            do
            {
                str2 = streamReader.ReadLine();
                if (str2 == null)
                    goto label_16;
            }
            while (!(str2.Substring(0, 1) == "S"));
            ushort num = 0;
            if (str2.Substring(1, 1) == "1")
                num = (ushort)2;
            else if (str2.Substring(1, 1) == "2")
                num = (ushort)3;
            else if (str2.Substring(1, 1) == "3")
                num = (ushort)4;
            if (num != (ushort)0)
            {
                if (uint.MaxValue == destAddr)
                {
                    string str3 = str2.Substring(4, (int)num * 2);
                    destAddr = Convert.ToUInt32(str3, 16);
                }
                str1 += str2.Substring(4 + (int)num * 2, str2.Length - (int)num * 2 - 6);
            }
        }
        while (str2.Substring(1, 1) == "9" || str2.Substring(1, 1) == "8" || !(str2.Substring(1, 1) == "7"));
        goto label_3;
    label_16:
        streamReader.Close();
        binBuffer = new byte[str1.Length / 2];
        int hex = (int)HexToBin.StringToHex(str1, ref binBuffer);
        return HexToBin.ExecResult.ExecOk;
    }

    public static HexToBin.ExecResult StringToHex(string str, ref byte[] hex)
    {
        int num = 0;
        int length = str.Length;
        if (str == null)
            return HexToBin.ExecResult.ExecError;
        for (int startIndex = 0; startIndex < length; startIndex += 2)
            hex[num++] = (byte)ushort.Parse(str.Substring(startIndex, 2), NumberStyles.HexNumber);
        return HexToBin.ExecResult.ExecOk;
    }

    // public static HexToBin.ExecResult HexToString(byte[] hex, ref string str)
    // {
    //     int length = hex.Length;
    //     if (hex == null)
    //         return HexToBin.ExecResult.ExecError;
    //     str = null;
    //     for (int index = 0; index < length; ++index)
    //         str += string.Format("{0:X2}", (object)hex[index]);
    //     return HexToBin.ExecResult.ExecOk;
    // }

    // public static byte SREC_Checksum(byte[] dataBuffer, int offset, int length)
    // {
    //     byte num = 0;
    //     if (dataBuffer == null || length <= 0)
    //         return 0;
    //     for (int index = 0; index < length; ++index)
    //         num += dataBuffer[offset + index];
    //     return (byte)((uint)byte.MaxValue - (uint)num);
    // }

    // public static byte HEX_Checksum(byte[] dataBuffer, int offset, int length)
    // {
    //     byte num = 0;
    //     if (dataBuffer == null || length <= 0)
    //         return 0;
    //     for (int index = 0; index < length; ++index)
    //         num += dataBuffer[offset + index];
    //     return (byte)(256U - (uint)num);
    // }

    public enum ExecResult
    {
        ExecOk = 1,
        ExecError = 2,
        ExecTimeout = 3,
        ExecErrorParameter = 4,
    }
}